﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraGrid.Views.Grid;

namespace QuanLyKho.PhongHoanThien
{
    public partial class DanhSachKCS : Form
    {
        public DanhSachKCS()
        {
            InitializeComponent();
            LoadData();
            SetPropertiesGrid();
        }

        public void LoadData()
        {
            DataTable kcs = DataAll.SelectAll("KCS inner join LenhSanXuat on KCS.SoChungTu = LenhSanXuat.SoChungTu inner join PhongMau_SanPham on KCS.IdSanPham = PhongMau_SanPham.IdSanPham",
                "*,LenhSanXuat.ID as 'LsxID',KCS.ID as 'KCSID',KCS.TrangThai as 'TrangThai1'",
                "WHERE KCS.TrangThai = " + Convert.ToInt32(TrangThai.KCS_DaDuyet));
            gridKCS.DataSource = kcs;
        }

        public void SetPropertiesGrid()
        {
            gvKCS.BestFitColumns();
            gvKCS.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            gvKCS.OptionsBehavior.Editable = false;
            gvKCS.OptionsSelection.MultiSelect = true;
            gvKCS.OptionsView.ShowGroupPanel = false;
            gvKCS.OptionsView.ShowFooter = true;
            gvKCS.OptionsFind.AlwaysVisible = true;
            gvKCS.Columns["LsxID"].Visible = false;
            gvKCS.Columns["KCSID"].Visible = false;

        }

        private void gvKCS_DoubleClick(object sender, EventArgs e)
        {
            if (gvKCS.RowCount > 0)
            {
                var dataview = (DataView)gvKCS.DataSource;
                var data = DataAll.SelectAll("HanhTrinh", "*", " WHERE SoChungTu='" + dataview[gvKCS.FocusedRowHandle]["SoChungTu"]
                    + "' AND TrangThai=" + Convert.ToInt32(TrangThai.KCS_HoanThienDaNhan));
                if (data.Rows.Count <= 0)
                {
                    DataAll.InsertAll("HanhTrinh", dataview[gvKCS.FocusedRowHandle]["MaHang"]
                                + "," + Convert.ToInt32(PhongBan.PhongHoanThien)
                                + "," + dataview[gvKCS.FocusedRowHandle]["SoChungTu"]
                                + "," + Convert.ToInt32(TrangThai.KCS_HoanThienDaNhan)
                                + "," + Main.UserID
                                + "," + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff")
                                + "," + dataview[gvKCS.FocusedRowHandle]["SoLuong"]
                                + "," + Convert.ToDateTime(dataview[gvKCS.FocusedRowHandle]["TuNgay"].ToString()).ToString("MM/dd/yyyy")
                                + "," + Convert.ToDateTime(dataview[gvKCS.FocusedRowHandle]["DenNgay"].ToString()).ToString("MM/dd/yyyy"));
                }
                CapNhatHoanThien.IdLenh = gvKCS.Columns["LsxID"].View.GetRowCellDisplayText(gvKCS.FocusedRowHandle, "LsxID");
                CapNhatHoanThien.IdKCS = gvKCS.Columns["KCSID"].View.GetRowCellDisplayText(gvKCS.FocusedRowHandle, "KCSID");
                var lss = new CapNhatHoanThien();
                lss.ShowDialog();
                LoadData();
                CapNhatHoanThien.IdLenh = "0";
            }
        }

        private void gvKCS_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "SXDenNgay")
            {
                TimeSpan songay = Convert.ToDateTime(e.CellValue) - DateTime.Now;
                if (0 < songay.Days && songay.Days < 5)
                {
                    e.Appearance.BackColor = Color.PaleGoldenrod;
                }
                if (songay.Days < 0)
                {
                    e.Appearance.BackColor = Color.DarkGray;
                }
            }
        }
    }
}
